package eu.codlab.twitteirb;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import eu.codlab.twitteirb.manage.url.LoadURL;

public class AuthenticatorHelper {
	public static final String ACCOUNT_TYPE = "eu.codlab.eirb.caslibrary.authenticator";
	private void log(String i1, String i2){
		//Log.d(i1,i2);
	}
	/**
	 * 
	 * @return the representing strings or null if login failed
	 */
	public String[] infos(String login, String password, String[] options){
		String [] retour = null;
		try{
			//--------------------------------------------------------------------------------------
			//TODO check if service can be auth.eirb.fr...
			String cas_enseirb="https://cas.enseirb-matmeca.fr/cas/login?service=http%3A%2F%2Ftwitt.eirb.fr%2F";
			log("appel 1 >>",cas_enseirb);

			String [] headers = new String[]{"Host","cas.enseirb-matmeca.fr",
					"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
					"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
					"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
					"Accept-Encoding","gzip, deflate",
					"Connection","keep-alive"};
			log("appel 1.2","headers");

			LoadURL chargement = new LoadURL(cas_enseirb);
			log("appel 1.3","loadurl");
			String res = chargement.loadGetUrl(headers);
			log("appel 1.4","res" + chargement.getStatus());

			ArrayList<String> cas_enseirb_cookie = chargement.getCookiesDefined();
			log("ezer"," "+res.length());
			String [] _split = res.split("post\" action=\"");
			if(_split.length > 1 || chargement.getLocation() != null){
				boolean has_redirection = chargement.getLocation() != null;

				if(!has_redirection){
					_split = _split[1].split("\"");

					log(" "," ");
					log("appel 2","https://cas.enseirb-matmeca.fr/cas/"+_split[0]);
					chargement = new LoadURL("https://cas.enseirb-matmeca.fr/cas/"+_split[0]);
					List<BasicNameValuePair> _list = new ArrayList<BasicNameValuePair>();

					String [] h3 = new String[]{
							"Host","cas.enseirb-matmeca.fr",
							"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
							"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
							"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
							"Accept-Encoding","deflate",
							"Connection","keep-alive",
							"Referer",cas_enseirb,
							"Cookie",cas_enseirb_cookie.size() > 0 ? cas_enseirb_cookie.get(0).split(";")[0]+";" : "N;"
					};
					try {
						_list.add(new BasicNameValuePair("username",login));
						_list.add(new BasicNameValuePair("password",password));
						//Log.d(" "+_login," zz"+_password);
					} catch (Exception e) {
						e.printStackTrace();
					}
					String []lt = res.split("lt\" value=\"");
					if(lt.length>1){
						lt = lt[1].split("\"");
						_list.add(new BasicNameValuePair("lt",lt[0]));
						//Log.d("lt",""+lt[0]);
					}
					_list.add(new BasicNameValuePair("_eventId","submit"));
					res = chargement.loadPostUrl(h3,_list);
				}
				log(">>>",chargement.getLocation());
				if(chargement.getLocation() != null && chargement.getLocation().indexOf("http://twitt.eirb.fr")<10){
					if(options == null)
						return new String[1];

					/**
					 * this array will have the different hash to use in the application
					 */
					retour = new String[options.length];
					//TODO
					//this means it is ok :p

					//-----------------------------------------
					//Log.d(" "," ");
					String redirection = chargement.getLocation();
					//Log.d("appel 3",redirection);


					headers=new String[]{
							"Host","twitt.eirb.fr",
							"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
							"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
							"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
							"Accept-Encoding","gzip, deflate",
							"Connection","keep-alive"
					};
					chargement = new LoadURL(redirection);
					chargement.loadGetUrl(headers);
					ArrayList<String> twitt_cookies = chargement.getCookiesDefined();

					if(chargement.getLocation() != null){
						//----------------------------------------------
						//Log.d(" "," ");
						String redirection_auth = chargement.getLocation();
						//Log.d("appel 4",redirection_auth);
						headers=new String[]{
								"Host","auth.eirb.fr",
								"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
								"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
								"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
								"Accept-Encoding","gzip, deflate",
								"Connection","keep-alive"
						};
						chargement = new LoadURL(redirection_auth);
						chargement.loadGetUrl(headers);
						ArrayList<String> auth_cookies = chargement.getCookiesDefined();
						if(auth_cookies == null || auth_cookies.size()==0)
							auth_cookies.add("N;");
						//-----------------------------------------------
						//Log.d(" "," ");
						//Log.d("appel 5","http://auth.eirb.fr/cas");
						headers=new String[]{
								"Host","auth.eirb.fr",
								"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
								"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
								"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
								"Accept-Encoding","gzip, deflate",
								"Connection","keep-alive",
								"Referer",redirection_auth,
								"Cookie",auth_cookies.size()>0 ? auth_cookies.get(0).split(";")[0]+";" : "N;"
						};

						//for(int i=0;i<headers.length;i+=2)
						//Log.d(headers[i],headers[i+1]);
						chargement = new LoadURL("http://auth.eirb.fr/cas");
						chargement.loadGetUrl(headers);
						if(chargement.getLocation() != null){
							//-----------------------------------------------
							//Log.d(" "," ");
							String login_check = "http://auth.eirb.fr"+chargement.getLocation();
							//Log.d("appel 6",login_check);
							chargement = new LoadURL(login_check);
							chargement.loadGetUrl(headers);

							if(chargement.getLocation() != null){
								//-----------------------------------------------
								//Log.d(" "," ");
								String nouveau_cas = chargement.getLocation();
								//Log.d("appel 7",nouveau_cas);
								headers=new String[]{"Host","cas.enseirb-matmeca.fr",
										"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
										"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
										"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
										"Accept-Encoding","gzip, deflate",
										"Connection","keep-alive",
										"Referer",redirection_auth,
										"Cookie",auth_cookies.size()>0 ? auth_cookies.get(0).split(";")[0]+";" : "N;"
								};
								chargement = new LoadURL(nouveau_cas);
								chargement.loadGetUrl(headers);
								if(chargement.getLocation() != null){
									//-----------------------------------------------
									//Log.d(" "," ");
									String nouveau_auth = chargement.getLocation();
									//Log.d("appel 8",nouveau_auth);
									headers=new String[]{
											"Host","auth.eirb.fr",
											"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
											"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
											"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
											"Accept-Encoding","gzip, deflate",
											"Connection","keep-alive",
											"Referer",redirection_auth,
											"Cookie",auth_cookies.get(0).split(";")[0]+";"
									};
									chargement = new LoadURL(nouveau_auth);
									chargement.loadGetUrl(headers);
									if(chargement.getLocation() != null){
										//Log.d(" "," ");
										String last_login = chargement.getLocation();
										//Log.d("appel 9",last_login);
										headers=new String[]{
												"Host","auth.eirb.fr",
												"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
												"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
												"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
												"Accept-Encoding","gzip, deflate",
												"Connection","keep-alive",
												"Referer",redirection_auth,
												"Cookie",auth_cookies.get(0).split(";")[0]+";"
										};
										chargement = new LoadURL(last_login);
										chargement.loadGetUrl(headers);
										if(chargement.getLocation() != null){
											//Log.d(" "," ");
											String last_cas = chargement.getLocation();
											//Log.d("appel 10",last_cas);
											headers=new String[]{
													"Host","auth.eirb.fr",
													"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
													"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
													"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
													"Accept-Encoding","gzip, deflate",
													"Connection","keep-alive",
													"Referer",redirection_auth,
													"Cookie",auth_cookies.get(0).split(";")[0]+";"
											};
											chargement = new LoadURL(last_cas);
											chargement.loadGetUrl(headers);

											if(chargement.getLocation() != null){
												//Log.d(" "," ");
												String beforelast_twitt = chargement.getLocation();
												//Log.d("appel 11",beforelast_twitt);
												headers=new String[]{
														"Host","twitt.eirb.fr",
														"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
														"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
														"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
														"Accept-Encoding","gzip, deflate",
														"Connection","keep-alive",
														"Referer",redirection_auth,
														"Cookie",twitt_cookies.get(0).split(";")[0]+";"
												};
												chargement = new LoadURL(beforelast_twitt);
												chargement.loadGetUrl(headers);

												if(chargement.getLocation() != null){
													//Log.d(" "," ");
													String last_twitt = chargement.getLocation();
													//Log.d("appel 12",last_twitt);
													headers=new String[]{
															"Host","twitt.eirb.fr",
															"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
															"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
															"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
															"Accept-Encoding","deflate",
															"Connection","keep-alive",
															"Referer",redirection_auth,
															"Cookie",twitt_cookies.get(0).split(";")[0]+";"
													};
													chargement = new LoadURL(last_twitt);
													//Log.d("twitt",chargement.loadGetUrl(headers));

													//now load trombi informations
													for(int i=0; options != null && i<options.length;i++){
														if(options[i] == null)
															options[i] ="twitt.eirb.fr";

														if(options[i].equals("twitt.eirb.fr")){
															retour[i] = twitt_cookies.get(0).split(";")[0];
														}else{
															log("login",i+" "+options[i]);
															headers=new String[]{
																	//ex trombi-dev.eirb.fr
																	"Host",options[i],
																	"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
																	"Accept","*/*",
																	"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
																	"Accept-Encoding","gzip, deflate",
																	"Connection","keep-alive",
																	"Referer","http://twitt.eirb.fr/"
															};
															chargement = new LoadURL("http://"+options[i]);//trombi-dev.eirb.fr/api.js");
															chargement.loadGetUrl(headers);
															ArrayList<String> c = chargement.getCookiesDefined();

															if(chargement.getLocation()!=null){
																headers=new String[]{
																		"Host",options[i],
																		"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
																		"Accept","*/*",
																		"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
																		"Accept-Encoding","gzip, deflate",
																		"Connection","keep-alive",
																		"Referer","http://twitt.eirb.fr/",
																		"Cookie",c.get(0).split(";")[0]+";"
																};
																chargement = new LoadURL("http://"+options[i]+"/login_check");
																chargement.loadGetUrl(headers);
																if(chargement.getLocation() != null){
																	//chargement is now auth
																	headers=new String[]{"Host","auth.eirb.fr",
																			"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
																			"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
																			"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
																			"Accept-Encoding","gzip, deflate",
																			"Connection","keep-alive",
																			"Referer","http://twitt.eirb.fr/",
																			"Cookie",auth_cookies.get(0).split(";")[0]+";"
																	};
																	chargement = new LoadURL(chargement.getLocation());
																	chargement.loadGetUrl(headers);
																	if(chargement.getLocation() != null){
																		headers = new String[]{
																				"Host",options[i],
																				"User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1",
																				"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
																				"Accept-Language","fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3",
																				"Accept-Encoding","gzip, deflate",
																				"Connection","keep-alive",
																				"Referer","http://twitt.eirb.fr/",
																				"Cookie",c.get(0).split(";")[0]+";"
																		};
																		chargement = new LoadURL(chargement.getLocation());
																		chargement.loadGetUrl(headers);
																		chargement = new LoadURL(chargement.getLocation());
																		chargement.loadGetUrl(headers);
																		//_master.onLoginOK(twitt_cookies.get(0).split(";")[0],
																		//		c.get(0).split(";")[0]);

																		retour[i]=c.get(0).split(";")[0];
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();

		}
		return retour;
		//_master.onLoginError();
	}

}
